package com.itheima.list;

import java.util.*;
import java.util.function.Consumer;

/*
回顾 List 的遍历方式
 */
public class Demo03 {

    public static void main(String[] args) {
        List<String> list= new ArrayList<>();
        list.add("蔡徐坤1");
        list.add("蔡徐坤2");
        list.add("蔡徐坤3");
        list.add("蔡徐坤4");
        list.add("蔡徐坤4");
        //使用迭代器遍历
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println("-------------------------");
        ListIterator<String> stringListIterator = list.listIterator(list.size()-1);
       /* stringListIterator.next();
        stringListIterator.next();
        stringListIterator.next();
        stringListIterator.next();
        stringListIterator.next();*/
        while (stringListIterator.hasPrevious()){
            System.out.println(stringListIterator.previous());
        }
        System.out.println("-------------------------");
        //增强for
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("-------------------------");
        //根据索引遍历 fori
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println("-------------------------");
        //用方法来遍历集合
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        list.forEach(s -> System.out.println(s));
    }
}
